/*
 * C/C++ KLIB Project
 * 
 * Copyright (c) 2012 Ken Zhang (cpp.china@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 */


#include "types.h"

void swap(void *a, void *b, int width)
{
	register byte tmp;
	register byte *_a = (byte *)a;
	register byte *_b = (byte *)b;

	if (_a != _b) {
		while (width-- > 0) {
			tmp = *_a;
			*_a++ = *_b;
			*_b++ = tmp;
		}
	}
}

void swap_int(void *base, int i, int j)
{
	register int tmp = ((int *)base)[i];
	((int *)base)[i] = ((int *)base)[j];
	((int *)base)[j] = tmp;
}

void swap_ptr(void *base, int i, int j)
{
	register void *tmp = ((ptr_t *)base)[i];
	((ptr_t *)base)[i] = ((ptr_t *)base)[j];
	((ptr_t *)base)[j] = tmp;
}

void swap_double(void *base, int i, int j)
{
	double tmp = ((double *)base)[i];
	((double *)base)[i] = ((double *)base)[j];
	((double *)base)[j] = tmp;
}

void swap_longdouble(void *base, int i, int j)
{
	long double tmp = ((long double *)base)[i];
	((long double *)base)[i] = ((long double *)base)[j];
	((long double *)base)[j] = tmp;
}
